/*
 * BaseSocket.h
 *
 *  Created on: 2012-12-14
 *      Author: yangentao@gmail.com
 */

#pragma once;
#include "NetUtil.h"

namespace yet {

class BaseSocket {
public:
	BaseSocket();
	BaseSocket(SOCKET s);
	virtual ~BaseSocket();
public:
	operator SOCKET();

	bool createTcpSocket();
	bool createUdpSocket();
	bool isValidSocket();

	void close();
	void setInvalid();
	// throw (SocketError);
	bool bind();
	bool bind(short localPort);
	bool bind(string localIP, u_short localPort);
	bool bind(InetAddr addr);

	//SOMAXCONN, 0x7fffffff
	bool listen();
	bool listen(int backlog);

	//throw (SocketError)
	//after bind!
	bool getSockName(InetAddr* outAddr);
	bool getPeerName(InetAddr* outAddr);

	//throw (SocketError)
	int connect(InetAddr peerAddr);
	int connect(string host, u_short peerPort);

	SOCKET accept(InetAddr* outAddr);

	int send(string buf);
	int send(const char* buffer, int size);
	int send(const char* buffer, int size, int flag);
	bool sendAll(const char* buffer, int size);
	bool sendAll(const char* buffer, int size, int flag);

	int recv(char* buffer, int size);
	int recv(char* buffer, int size, int flag);

	int sendTo(InetAddr peerAddr, const char* buf, int len);
	int recvFrom(InetAddr* outPeerAddr, char* buf, int len);

	int select(FdSet* readSet, FdSet* writeSet, FdSet* exceptionSet, int seconds, int usec = 0);
	int selectInfinite(FdSet* readSet, FdSet* writeSet, FdSet* exceptionSet);
	int ioctl(long cmd, u_long* arg);
	int ioctlNoBlock(u_long noBlock);
	int ioctlRead(u_long* n);
public:
	SOCKET sock;
};



} /* namespace yet */
